package com.fcat.common.user.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fcat.base.common.security.HashUtil;
import com.fcat.base.common.util.ArrayUtil;
import com.fcat.base.common.util.StrUtil;
import com.fcat.common.user.service.DictService;
import com.fcat.common.user.service.ResourceService;
import com.fcat.common.user.vo.ResourceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private DictService dictService;


    @Override
    public Map<String, ResourceInfo> getExResources(JSONObject resourceObject, List<ResourceInfo> resourceInfoList) {
        String httpIcon = dictService.getNameByValue("httpIcon");
        //标题和序号
        Map<String, ResourceInfo> resourceMap = new HashMap<>();
        JSONArray data = resourceObject.getJSONArray("data");
        //List<ResourceInfo> resourceList = new ArrayList<>();
        if (data.size() == 0) {
            return null;
        }
        for (Object obj : data) {
            ResourceInfo resourceInfo = new ResourceInfo();
            JSONObject jsonObject = (JSONObject) obj;
            resourceInfo.setResourceId(jsonObject.getString("id"));
            resourceInfo.setDisplayName(jsonObject.getString("displayName"));
            resourceInfo.setName(jsonObject.getString("name"));
            resourceInfo.setType(jsonObject.getString("type"));
            resourceInfo.setUri(jsonObject.getString("uri"));
            resourceInfo.setIconUri((httpIcon == null ? "" : httpIcon) + jsonObject.getString("iconUri"));
            JSONObject jsonObj = jsonObject.getJSONObject("attributes");
            String parentMenu = "";
            if (jsonObj != null) {
                JSONArray jsonArray = jsonObj.getJSONArray("parent_menu");
                if (jsonArray != null) {
                    parentMenu = jsonArray.get(0).toString();
                }
                jsonArray = jsonObj.getJSONArray("first_sort");
                if (jsonArray != null) {
                    resourceInfo.setFirstSort(Integer.parseInt(jsonArray.get(0).toString()));
                }
                jsonArray = jsonObj.getJSONArray("second_sort");
                if (jsonArray != null) {
                    resourceInfo.setSecondSort(Integer.parseInt(jsonArray.get(0).toString()));
                }
                jsonArray = jsonObj.getJSONArray("third_sort");
                if (jsonArray != null) {
                    resourceInfo.setThirdSort(Integer.parseInt(jsonArray.get(0).toString()));
                }
            }
            if (!StrUtil.isBlank(parentMenu)) {
                resourceInfo.setParentMenus(parentMenu);
            }
            String leve1Title = "";
            if (!resourceMap.containsKey(resourceInfo.getType())) {
                ResourceInfo tmpResourceInfo = new ResourceInfo();
                tmpResourceInfo.setParentId(null);
                String md5 = HashUtil.md5(resourceInfo.getType().getBytes());
                tmpResourceInfo.setId(md5);
                tmpResourceInfo.setDisplayName(resourceInfo.getType());
                tmpResourceInfo.setName(resourceInfo.getName());
                tmpResourceInfo.setUri(resourceInfo.getUri());
                tmpResourceInfo.setIconUri(resourceInfo.getIconUri());
                tmpResourceInfo.setResourceId(resourceInfo.getResourceId());
                if (resourceInfo.getFirstSort() != null) {
                    tmpResourceInfo.setIndex(resourceInfo.getFirstSort());
                }
                resourceMap.put(resourceInfo.getType(), tmpResourceInfo);
            } else {
                if (resourceInfo.getFirstSort() != null) {
                    ResourceInfo tmpResourceInfo = resourceMap.get(resourceInfo.getType());
                    tmpResourceInfo.setIndex(resourceInfo.getFirstSort());
                }
            }

            if (StrUtil.isBlank(parentMenu)) {//判断2ji目录，表示没有2ji
                ResourceInfo tmpResourceInfo = new ResourceInfo();
                String title = resourceInfo.getType() + resourceInfo.getDisplayName();
                String md5 = HashUtil.md5(title.getBytes());
                tmpResourceInfo.setId(md5);
                tmpResourceInfo.setDisplayName(resourceInfo.getDisplayName());
                tmpResourceInfo.setName(resourceInfo.getName());
                tmpResourceInfo.setParentId(HashUtil.md5(resourceInfo.getType().getBytes()));
                tmpResourceInfo.setIndex(resourceInfo.getThirdSort());
                tmpResourceInfo.setUri(resourceInfo.getUri());
                tmpResourceInfo.setIconUri(resourceInfo.getIconUri());
                tmpResourceInfo.setResourceId(resourceInfo.getResourceId());
                resourceMap.put(title, tmpResourceInfo);
            } else {
                String title = resourceInfo.getType() + parentMenu;
                String level2md5 = HashUtil.md5(title.getBytes());
                if (!resourceMap.containsKey(title)) {
                    ResourceInfo tmpResourceInfo = new ResourceInfo();
                    tmpResourceInfo.setId(level2md5);
                    tmpResourceInfo.setParentId(HashUtil.md5(resourceInfo.getType().getBytes()));
                    tmpResourceInfo.setDisplayName(parentMenu);
                    tmpResourceInfo.setUri(resourceInfo.getUri());
                    tmpResourceInfo.setIconUri(resourceInfo.getIconUri());
                    tmpResourceInfo.setResourceId(resourceInfo.getResourceId());
                    if (resourceInfo.getSecondSort() != null) {
                        tmpResourceInfo.setIndex(resourceInfo.getSecondSort());
                    }
                    resourceMap.put(title, tmpResourceInfo);
                } else {
                    if (resourceInfo.getSecondSort() != null) {
                        ResourceInfo tmpResourceInfo = resourceMap.get(title);
                        tmpResourceInfo.setIndex(resourceInfo.getSecondSort());
                    }
                }
                ResourceInfo tmpResourceInfo = new ResourceInfo();
                title = resourceInfo.getType() + parentMenu + resourceInfo.getDisplayName();
                String md5 = HashUtil.md5(title.getBytes());
                tmpResourceInfo.setId(md5);
                tmpResourceInfo.setParentId(level2md5);
                tmpResourceInfo.setDisplayName(resourceInfo.getDisplayName());
                tmpResourceInfo.setName(resourceInfo.getName());
                tmpResourceInfo.setIndex(resourceInfo.getThirdSort());
                tmpResourceInfo.setUri(resourceInfo.getUri());
                tmpResourceInfo.setIconUri(resourceInfo.getIconUri());
                tmpResourceInfo.setResourceId(resourceInfo.getResourceId());
                resourceMap.put(title, tmpResourceInfo);
            }
            resourceInfoList.add(resourceInfo);
        }
        return resourceMap;
    }

    @Override
    public List<ResourceInfo> getSubResources(Map<String, ResourceInfo> map, String parentId) {
        List<ResourceInfo> resourceInfoList = new ArrayList<>();
        if (!ArrayUtil.isEmpty(map)) {
            for (Map.Entry<String, ResourceInfo> entry : map.entrySet()) {
                ResourceInfo resourceInfo = entry.getValue();
                if (parentId == null) {
                    if (resourceInfo.getParentId() == null) {
                        resourceInfoList.add(resourceInfo);
                    }
                } else {
                    if (resourceInfo.getParentId() != null && resourceInfo.getParentId().equals(parentId)) {
                        resourceInfoList.add(resourceInfo);
                    }
                }
            }
        }
        return resourceInfoList;
    }
}